package com.javaweb.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ExceptionHandlingAspect {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandlingAspect.class);

    @Around("@within(NPECatch) || @annotation(NPECatch)")
    public Object handleNPE(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            return joinPoint.proceed();
        } catch (NullPointerException e) {
            String methodName = joinPoint.getSignature().getName();
            logger.error("Exception in method: {}, message: {}", methodName, e.getMessage());
            return null; // 或其他适当的返回值
        } catch (Throwable t) {
            logger.error("Unexpected exception: {}", t.getMessage());
            throw t; // 重新抛出其他异常
        }
    }
}
